package org.jeecg.modules.paten.webController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.paten.entity.JkPatentInfo;
import org.jeecg.modules.paten.service.IJkPatentInfoService;
import org.jeecg.modules.util.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Tag(name="前端专利")
@RestController
@RequestMapping("/api/patent")
@Slf4j
public class JKPatenWebController {

    @Autowired
    private IJkPatentInfoService jkPatentInfoService;
    @Autowired
    DictUtils dictUtils;

    /**
     * 分页列表查询
     *
     * @param jkPatentInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "专利-分页列表查询")
    @Operation(summary="专利-分页列表查询")
    @GetMapping(value = "/list")
    @IgnoreAuth
    public Result<IPage<JkPatentInfo>> queryPageList(JkPatentInfo jkPatentInfo,
                                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                     HttpServletRequest req) {

        QueryWrapper<JkPatentInfo> queryWrapper = QueryGenerator.initQueryWrapper(jkPatentInfo, req.getParameterMap());
        queryWrapper.orderByAsc("sn");
        Page<JkPatentInfo> page = new Page<JkPatentInfo>(pageNo, pageSize);
        IPage<JkPatentInfo> pageList = jkPatentInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "专利-通过id查询")
    @Operation(summary="专利-通过id查询")
    @GetMapping(value = "/queryById")
    @IgnoreAuth
    public Result<JkPatentInfo> queryById(@RequestParam(name="id",required=true) String id) {
        JkPatentInfo jkPatentInfo = jkPatentInfoService.getById(id);
        if(jkPatentInfo==null) {
            return Result.error("未找到对应数据");
        }

        String patentField = dictUtils.getDictItemName("patent_field_type",jkPatentInfo.getPatentField());
        jkPatentInfo.setPatentField(patentField);

        String patentType = dictUtils.getDictItemName("patent_type",jkPatentInfo.getPatentType());
        jkPatentInfo.setPatentType(patentType);


        return Result.OK(jkPatentInfo);
    }
}
